var home = require("../../utils/util.js");
var url = home.home;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    money:0,
    bgcolor: home.getKey('bgcolor'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#'+home.getKey('bgcolor'),
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
  },

  getmoney(){
    let that = this;
    wx.request({
      url: url+'app/widthManagment/getCanWithdrawalAmount',
      data:{
        storeId:home.getKey('storeId'),
        stockId:home.getKey('stockId'),
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function(res){
        that.setData({
          money:res.data.result.canWithdrawal
        })
      }
    })
  },

  //申请提现
  WithMoney(){
    let that = this;
    let applyAmount = this.data.value;
    let money = this.data.money;
    if (applyAmount == 0) {
      home.wxMessage('提现金额不能为0','none');
      return false;
    }
    if (money < applyAmount) {
      home.wxMessage('金额不足','none');
      return false;
    }
    wx.request({
      url: url + 'app/widthManagment/applyWithdrawal?storeId='+home.getKey('storeId')+'&stockId='+home.getKey('stockId')+'&applyAmount='+applyAmount,
      data:{
        storeId:home.getKey('storeId'),
        stockId:home.getKey('stockId'),
        applyAmount:applyAmount
      },
      method: 'PUT',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.result,
          });
          money = (money - applyAmount).toFixed(2);
          that.setData({money:money,value:''});
        } else {
          wx.showToast({
            title: '提现失败,请稍后再试',
            icon: 'none'
          })
        }
      }
    })
  },

  onChange(e) {
    if (e.detail != '') {
      if (parseFloat(e.detail).toString() == 'NaN' || (e.detail / 2).toString() == 'NaN') {
        wx.showToast({
          title: '请输入正确的数字',
          icon:'none'
        });
        return false;
      }
    }
    this.setData({
      value: e.detail
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getmoney();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})